﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSemIII.Administrator.Survey
{
    public partial class QuestionManage : System.Web.UI.Page
    {
        private SurveyProcess surveyProcess;
        private Entity.Question question;
        private int questionID;
        protected string Values;
        private string[] words;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString.Count == 0 || Request.QueryString.Count > 1)
            {
                ClientScript.RegisterStartupScript(GetType(), "ClosePopup", "window.close();", true);
            }
            else
            {
                surveyProcess = new SurveyProcess();
                if (Request.QueryString["questionid"] != null)
                {
                    btSubmit.Text = "Edit Question";
                    words = surveyProcess.getListAnswer(Convert.ToInt32(Request.QueryString["questionid"]));
                    if (!Page.IsPostBack)
                    {
                        LoadContactControls();
                        this.getQuestion();
                    }
                }
                else if (Request.QueryString["surveyid"] != null)
                {
                    btSubmit.Text = "Create New Question";
                }
            }
        }
        private void LoadContactControls()
        {
            foreach (string w in words)
            {
                TextBox tb = new TextBox();
                tb.Text = w;
                tb.ID = "DynamicTextBox";
                tb.Style.Add("width", "80%");
                Button bt = new Button();
                bt.CssClass = "buttonS bLightBlue";
                bt.OnClientClick = "RemoveTextBox(this)";
                bt.Text = "Remove";
                Panel myChildPanel = new Panel();
                myChildPanel.Controls.Add(tb);
                myChildPanel.Controls.Add(new LiteralControl("&nbsp&nbsp&nbsp"));
                myChildPanel.Controls.Add(bt);
                myChildPanel.Controls.Add(new LiteralControl("<br/>"));
                myChildPanel.Controls.Add(new LiteralControl("<br/>"));
                TextBoxContainer.Controls.Add(myChildPanel);
            }
        }

        private void getQuestion()
        {
            question = new Entity.Question();
            questionID = Convert.ToInt32(Request.QueryString["questionid"].ToString());
            question = surveyProcess.getQuestionByID(questionID);
            lbQuestionNumber.InnerText = "Question Number " + question.QuestionID;
            txtQuestion.Text = question.QuestionDetail;
            if (question.QuestionTypeID == 1)
            {
                answerForm.Visible = true;
                rbRadio.Checked = true;
            }
            else if (question.QuestionTypeID == 2)
            {
                rbComment.Checked = true;
                answerForm.Visible = false;
            }
            else
            {
                answerForm.Visible = true;
                rbCheckBox.Checked = true;
            }
        }
        protected void btSubmit_Click(object sender, EventArgs e)
        {
            question = new Entity.Question();
            string[] textboxValues = Request.Form.GetValues("DynamicTextBox");
            if (rbRadio.Checked == true)
            {
                question.QuestionTypeID = 1;
                foreach (string w in textboxValues)
                {
                    if (w == textboxValues[0])
                    {
                        question.Answers += w;
                    }
                    else
                    {
                        question.Answers += "#" + w;
                    }
                }
            }
            if (rbComment.Checked == true)
            {
                question.QuestionTypeID = 2;
                question.Answers = "";
            }
            if (rbCheckBox.Checked)
            {
                question.QuestionTypeID = 3;
                foreach (string w in textboxValues)
                {
                    if (w == textboxValues[0])
                    {
                        question.Answers += w;
                    }
                    else
                    {
                        question.Answers += "#" + w;
                    }
                }
            }
            question.QuestionDetail = txtQuestion.Text;
            if (this.validateQuestion())
            {
                if (btSubmit.Text == "Edit Question")
                {
                    question.QuestionID = Convert.ToInt32(Request.QueryString["questionid"].ToString());
                    int result = surveyProcess.updateQuestionByQuestion(question);
                    if (result == 10)
                    {
                        ClientScript.RegisterStartupScript(GetType(), "ClosePopup", "window.opener.location.reload();window.close();", true);
                    }
                    else
                    {
                        lbError.ForeColor = System.Drawing.Color.Red;
                        lbError.Text = "Error in update question!";
                    }
                }
                else if (btSubmit.Text == "Create New Question")
                {
                    int surveyID = Convert.ToInt32(Request.QueryString["surveyid"].ToString());
                    List<Entity.Question> listQuestion = new List<Entity.Question>();
                    question.SurveyID = surveyID;
                    listQuestion.Add(question);
                    int result = surveyProcess.insertQuestionToSurvey(listQuestion);
                    if (result == 10)
                    {
                        ClientScript.RegisterStartupScript(GetType(), "ClosePopup", "window.opener.location.reload();window.close();", true);
                    }
                    else
                    {
                        lbError.ForeColor = System.Drawing.Color.Red;
                        lbError.Text = "Error in create question!";
                    }
                }
            }
        }

        private bool validateQuestion()
        {
            bool flag = true;
            if (txtQuestion.Text == "" || txtQuestion.Text == null)
            {
                lbError.ForeColor = System.Drawing.Color.Red;
                lbError.Text = "Please enter question!";
                flag = false;
            }
            else if (rbComment.Checked == false)
            {
                string[] textboxValues = Request.Form.GetValues("DynamicTextBox");
                if (textboxValues == null || textboxValues.Length < 1)
                {
                    lbError.Text = "Please add answers!";
                    flag = false;
                }
                else
                {
                    foreach (string w in textboxValues)
                    {
                        if (w == "" || w == null)
                        {
                            lbError.Text = "Please enter answers!";
                            flag = false;
                        }
                    }
                }
            }
            return flag;
        }

        protected void rbRadio_CheckedChanged(object sender, EventArgs e)
        {
            if (rbRadio.Checked == true)
            {
                answerForm.Visible = true;
            }
        }

        protected void rbCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (rbCheckBox.Checked == true)
            {
                answerForm.Visible = true;
            }
        }

        protected void rbComment_CheckedChanged(object sender, EventArgs e)
        {
            if (rbComment.Checked == true)
            {
                answerForm.Visible = false;
            }
        }

    }
}